package com.future.mayi.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.adapter.RedPacketAdapter;
import com.future.mayi.app.App;
import com.future.mayi.model.RedPacketModel;
import com.future.mayi.model.interfaces.OnItemClickListener;
import com.future.mayi.response.RedPackResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SelectReadPacketActivity extends BaseTitleActivity implements XRecyclerView.LoadingListener, OnItemClickListener {
    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;
    @BindView(R.id.iv_null)
    ImageView ivNull;
    @BindView(R.id.tv_enter)
    TextView tvEnter;


    private double money;
    private String goods_ids;
    private RedPacketAdapter adapter;


    public static void startActivity(Activity activity, double money, String goods) {
        activity.startActivityForResult(new Intent(activity, SelectReadPacketActivity.class).putExtra("money", money).putExtra("goods", goods), 234);
    }

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("选择红包");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_coupons_select);
    }

    @Override
    protected void initView() {
        recyclerView.setHasFixedSize(true);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new RedPacketAdapter(this, recyclerView, 2);
        recyclerView.setAdapter(adapter);
        recyclerView.setEmptyView(ivNull);
        recyclerView.setLoadingMoreEnabled(false);
    }

    @Override
    protected void initDates() {
        money = getIntent().getDoubleExtra("money", 0);
        adapter.setMoney(money);
        goods_ids = getIntent().getStringExtra("goods");
    }

    @Override
    protected void initListener() {
        recyclerView.setLoadingListener(this);
        adapter.setOnItemClickListener(this);
        recyclerView.refresh();
    }

    @SuppressWarnings("unchecked")
    private void getData() {
        new HttpRequest<RedPackResponse>()
                .with(this)
                .setApiCode(ApiCst.getRedpackList)
                .addParam("status", 0)
                .addParam("p", 0)
                .addParam("psize", 10000)
                .addParam("token", App.getToken(mContext))
                .addParam("goods_ids", goods_ids)
                .setListener(new HttpRequest.OnNetworkListener<RedPackResponse>() {
                    @Override
                    public void onSuccess(RedPackResponse response) {
                        HCUtils.refreshListForPage(recyclerView, adapter, response.getList(), 0, 10);
                    }

                    @Override
                    public void onFail(String message, RedPackResponse response) {
                        HCUtils.refreshFail(recyclerView, 0, mContext, message);
                    }
                }).start(new RedPackResponse());
    }


    @Override
    public void onRefresh() {
        getData();
    }

    @Override
    public void onLoadMore() {
    }

    @Override
    public void OnItem(View view, int position) {

    }

    @Override
    public void OnLongItem(View view, int position) {

    }


    @OnClick(R.id.tv_enter)
    public void onViewClicked() {
        if (adapter != null && adapter.getItemCount() > 0) {
            List<RedPacketModel> list = adapter.getSelect();
            if (list != null && list.size() > 0) {
                double subMoney = 0d;
                for (RedPacketModel redPacketModel : list) {
                    subMoney = subMoney + redPacketModel.getPar_value();
                }
                if (subMoney > money) {
                    toast("红包金额大于订单金额！");
                    return;
                }
            }
            Intent intent = new Intent();
            intent.putExtra("redPacket", (Serializable) list);
            setResult(Activity.RESULT_OK, intent);
        }
        finish();
    }
}
